<template>
  <div>
    <el-form
      ref="formBizSubject"
      :model="formBizSubject"
      label-width="75px"
      :size="defaultFormItemSize"
      label-position="right"
      @submit.native.prevent
    >
      <filter-box :item-width="350" @search="refreshFormBizSuject(true)" @reset="onReset">
        <el-form-item label="题干" prop="formFilter.subjectTitle">
          <el-input
            class="filter-item"
            v-model="formBizSubject.formFilter.subjectTitle"
            :clearable="true"
            placeholder="题干"
          />
        </el-form-item>
      </filter-box>
    </el-form>
    <table-box
      class="page-table"
      :data="formBizSubject.SysUser.impl.dataList"
      :size="defaultFormItemSize"
      @sort-change="formBizSubject.SysUser.impl.onSortChange"
      @refresh="refreshFormBizSuject(true)"
      :seq-config="{
        startIndex:
          (formBizSubject.SysUser.impl.currentPage - 1) * formBizSubject.SysUser.impl.pageSize
      }"
    >
      <el-button
        slot="operator"
        type="primary"
        icon="el-icon-check"
        :size="defaultFormItemSize"
        @click="genreate()"
        >确认</el-button
      >

      <vxe-column title="序号" type="seq" width="50px" />
      <vxe-column title="题干" field="subjectTitle" />
      <vxe-column title="分值" field="subjectScore" />
      <vxe-column title="操作" fixed="right" width="230px">
        <template slot-scope="scope">
          <el-button
            class="btn-table-edit"
            v-show="!judgeSelected(scope.row.subjectId)"
            type="text"
            :size="defaultFormItemSize"
            @click="selectRow(scope.row)"
          >
            选择
          </el-button>
          <el-button
            class="btn-table-delete"
            v-show="judgeSelected(scope.row.subjectId)"
            type="text"
            :size="defaultFormItemSize"
            @click="freeRow(scope.row)"
          >
            取消
          </el-button>
        </template>
      </vxe-column>

      <el-row slot="pagination" type="flex" justify="end" style="margin-top: 16px">
        <el-pagination
          :total="formBizSubject.SysUser.impl.totalCount"
          :current-page="formBizSubject.SysUser.impl.currentPage"
          :page-size="formBizSubject.SysUser.impl.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, prev, pager, next, sizes"
          @current-change="formBizSubject.SysUser.impl.onCurrentPageChange"
          @size-change="formBizSubject.SysUser.impl.onPageSizeChange"
        >
        </el-pagination>
      </el-row>
    </table-box>
  </div>
</template>

<script>
import { DropdownWidget, TableWidget } from '@/utils/widget.js';
/* eslint-disable-next-line */
import { uploadMixin, statsDateRangeMixin, cachePageMixin, cachedPageChildMixin } from '@/core/mixins';
/* eslint-disable-next-line */
import { BizSubjectController } from '@/api';

export default {
  name: 'formsChooseBizEvluateSurvey',
  props: {
    rowData: Object
  },
  mixins: [uploadMixin, statsDateRangeMixin, cachePageMixin],
  data() {
    return {
      deptIdPath: [],
      selected: [],
      formBizSubject: {
        formFilter: {
          subjectTitle: ''
        },
        formFilterCopy: {
          subjectTitle: ''
        },
        SysUser: {
          impl: new TableWidget(
            this.loadBizSubjectData,
            this.loadBizSubjectVerify,
            true,
            false,
            'createTime',
            1
          )
        },
        isInit: false,
        subjectType: {
          impl: new DropdownWidget(this.loadSubjectTypeDropdownList)
        }
      }
    };
  },
  methods: {
    judgeSelected(subjectId) {
      return this.selected.some(item => item.subjectId === subjectId);
    },
    onReset() {
      this.$refs.formBizSubject.resetFields();
      this.refreshFormBizSuject(true);
    },
    loadBizSubjectData(params) {
      params.subjectFilter = {
        subjectTitle: this.formBizSubject.formFilterCopy.subjectTitle,
        moduleType: 5
      };
      return new Promise((resolve, reject) => {
        BizSubjectController.findByPage(this, params)
          .then(res => {
            resolve({
              dataList: res.data.dataList,
              totalCount: res.data.totalCount
            });
          })
          .catch(e => {
            reject(e);
          });
      });
    },
    loadBizSubjectVerify() {
      this.formBizSubject.formFilterCopy.subjectTitle = this.formBizSubject.formFilter.subjectTitle;
      return true;
    },
    refreshFormBizSuject(reloadData = false) {
      // 重新获取数据组件的数据
      if (reloadData) {
        this.formBizSubject.SysUser.impl.refreshTable(true, 1);
      } else {
        this.formBizSubject.SysUser.impl.refreshTable();
      }
      this.formBizSubject.isInit = true;
    },
    onResume() {
      this.refreshFormBizSuject();
    },
    initFormData() {},
    formInit() {
      this.initFormData();
      this.refreshFormBizSuject();
    },
    selectRow(rowData) {
      if (!this.judgeSelected(rowData.subjectId)) {
        this.selected.push(rowData);
      }
    },
    freeRow(rowData) {
      if (this.judgeSelected(rowData.subjectId)) {
        this.selected = this.selected.filter(item => item.subjectId !== rowData.subjectId);
      }
    },
    onCancel(isSuccess = false) {
      if (this.observer != null) {
        this.observer.cancel(isSuccess, this.selected);
      }
    },
    genreate() {
      this.onCancel(true);
    }
  },
  created() {
    if (this.rowData != null) {
      this.selected = this.rowData.includedSubjects;
    }
    this.formInit();
  }
};
</script>
